package day10.homework;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;

public class Server {
    public static void main(String[] args) throws IOException {

        List<Student> Stus = new ArrayList<>();
        Stus.add(new Student(12, "fjeogij"));
        Stus.add(new Student(23, "妇女节危机"));
        while (true) {
            //serverSocket ：服务端套接字
            ServerSocket ss = new ServerSocket(8881);
            //2调用accept 返回客户端套接字
            Socket s = ss.accept();

            //3获得输入流对象
            InputStream is = s.getInputStream();
            Map<Integer, Student> map = new HashMap<>();
            ObjectInputStream ois = new ObjectInputStream(is);
            try {
                map = (Map<Integer, Student>) ois.readObject();
                Set<Integer> se = map.keySet();
                for (Integer integer : se) {
                    if (integer == 1) {
                        Stus.add(map.get(integer));
                        System.out.println("添加成功");
                    } else if (integer == 2) {
                        Stus.remove(map.get(integer));
                        System.out.println("删除成功");
                    } else if (integer == 3) {
                        System.out.println("查看数据");
                        for (Student stu : Stus) {
                            System.out.println(stu);
                        }

                    } else if (integer == 4) {
                        for (Student stu : Stus){
                            if (stu.getId() == map.get(integer).getId()) {
                                Stus.remove(stu);
                                Stus.add(map.get(integer));
                                System.out.println("修改成功");
                                break;
                            }
                        }
                    } else {
                        System.out.println("输入有误,请重新输入");
                    }
                }
//                Stus.add(st);
                System.out.println("-------------------------------------------------");
//                for (Student stu : Stus) {
//                    System.out.println(stu);
//                }

            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                ss.close();
                s.close();
                is.close();
                ois.close();
            }
        }
    }
}
